<?php  

if( ! defined('BASEPATH')) exit('No direct script access allowed');

class daily_report extends CI_Controller{
	function __construct(){
		parent::__construct();
		
		// check if user logged in 
		if(!$this->ion_auth->logged_in()){
			redirect('auth/login');
		}		
		$this->load->library('form_validation');
		$this->load->model('report/daily_model');		
	}
		
	
	function report(){
		//echo "i am here";exit;		
		$meta['page_title'] = "Daily Report";
		$customers = $this->daily_model->get_customers();
		$products = $this->daily_model->get_product();
		$suppliers = $this->daily_model->get_suppliers();
		if(count($customers)>0){
			$this->data['customers']= $customers;	
			
		}else{
				$this->data['customers']=array();	
		}
		
		if(count($products)> 0){
			$this->data['products']= $products;	
			
		}else{
			$this->data['products']= array();	
		}
		if(count($suppliers) > 0){
			$this->data['suppliers']= $suppliers;	
		}else{
			$this->data['suppliers']= array();	
		}
		
		//=$this->daily_model->get_product();
		//$this->data['$suppliers']=$this->daily_model->get_suppliers();
		//print_r($this->data['customers']);exit;
		/*if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
		{
		redirect('auth', 'refresh');
		}for checking the authentication*/

		//validate form input
		//$this->form_validation->set_rules('code', 'Invoice Code', 'required|is_unique[branch.code]');
		//check to see if we are creating the user
		//redirect them back to the admin page
		//$this->session->set_flashdata('message', $this->ion_auth->messages());
		//redirect("auth", 'refresh');
	
		$this->data['to_date'] = array(
			'name'  => 'to_date',
			'id'    => 'to_date',
			'type'  => 'text',
			'class' => 'datepicker',
			'value' => $this->form_validation->set_value('to_date'),
		);
		$this->data['from_date'] = array(
			'name'  => 'from_date',
			'id'    => 'from_date',
			'type'  => 'text',
			'class' => 'datepicker',
			'value' => $this->form_validation->set_value('from_date'),
		);
			
		$this->data['id_product'] = array(
			'name'  => 'id_product',
			'id'    => 'id_product',
			'type'  => 'select',
			'value' => $this->form_validation->set_value('id_product'),
			
		);
		$this->data['id_customer'] = array(
			'name'  => 'id_customer',
			'id'    => 'id_customer',
			'type'  => 'select',
			'value' => $this->form_validation->set_value('id_customer'),
			
		);
		
		$this->data['id_supplier'] = array(
			'name'  => 'id_supplier',
			'id'    => 'id_supplier',
			'type'  => 'select',
			'value' => $this->form_validation->set_value('id_supplier'),
			
		);
		
		$this->load->view('common/header', $meta);
		$this->load->view('report/daily_report/daily_report', $this->data);
		$this->load->view('common/footer');
		
	}
	
	
	
	function prepare_report(){
		
		if($_REQUEST['id_customer']!= '0' || $_REQUEST['id_supplier'] != '0'){
			
			if(($_REQUEST['id_customer']!='0' && $_REQUEST['type'] == 'purchase')  || ($_REQUEST['id_supplier']!='0' && $_REQUEST['type'] == 'sale') ){
				echo "Invalid Request!!";
			}else{
				if($_REQUEST['id_customer']!='0'){
					if($_REQUEST['id_customer']=='all'){
						$this->data['all_sales'] = $this->daily_model->getall_sales($_REQUEST['id_customer'],$_REQUEST['from_date'],$_REQUEST['to_date']);
					}else{
						$this->data['all_sales'] = $this->daily_model->getall_sales($_REQUEST['id_customer'],$_REQUEST['from_date'],$_REQUEST['to_date']);
					}
					$this->load->view('report/reports/all_sales',$this->data);
				}
				else{
					if($_REQUEST['id_supplier']=='all'){
						$this->data['all_purchases'] = $this->daily_model->getall_purchase($_REQUEST['id_supplier'],$_REQUEST['from_date'],$_REQUEST['to_date']);
					}else{ 
						$this->data['all_purchases'] = $this->daily_model->getall_purchase($_REQUEST['id_supplier'],$_REQUEST['from_date'],$_REQUEST['to_date']);
					
					}	
					$this->load->view('report/reports/all_purchases',$this->data);
		
				}	
			}
		}else{
			if($_REQUEST['id_product']=='0'){
				if($_REQUEST['type']=='sale'){
					$this->data['all_sales'] = $this->daily_model->get_sale($_REQUEST['from_date'],$_REQUEST['to_date']);
								$this->load->view('report/reports/all_sales',$this->data);
		
					
				}elseif($_REQUEST['type']=='purchase'){ 
					$this->data['all_purchases'] = $this->daily_model->get_purchase($_REQUEST['from_date'], $_REQUEST['to_date']);
					$this->load->view('report/reports/all_purchases',$this->data);
		
				}/*elseif($_REQUEST['type']=='stock'){
					$results = $this->daily_model->get_product_stock($_REQUEST['id_product'],$_REQUEST['from_date'], $_REQUEST['to_date']);
						$this->data['stocks']=$results;
						$this->data['product_code']=$_REQUEST['id_product'];
					//print_r($this->data);exit;
						$this->load->view('report/reports/particular_stock_product',$this->data);
				
				}*/	
					
			}else{
				if($_REQUEST['id_product']=='all'){
					if($_REQUEST['type']=='sale'){
						$this->data['all_sales_product'] =$this->data['all_sales_product'] = $this->daily_model->get_product_sale($_REQUEST['id_product'],$_REQUEST['from_date'], $_REQUEST['to_date']);
						$this->load->view('report/reports/all_sales_product',$this->data);
					}elseif($_REQUEST['type']=='purchase'){
						$this->data['all_purchase_product'] = $this->daily_model->get_product_purchase($_REQUEST['id_product'],$_REQUEST['from_date'], $_REQUEST['to_date']);
						$this->load->view('report/reports/all_purchase_product',$this->data);
					}elseif($_REQUEST['type']='stock'){
							$results = $this->daily_model->get_product_stock($_REQUEST['id_product'],$_REQUEST['from_date'], $_REQUEST['to_date']);
						
							$this->data['stocks']=$results;
						
							$this->load->view('report/reports/all_stock_product',$this->data);				
					}/*else{
						$this->daily_model->get_product_stock($_REQUEST['id_product'],$_REQUEST['from_date'], $_REQUEST['to_date']);
						$this->load->view('report/reports/all_purchase_product',$this->data);
					}	*/
			
				}else{ 
					if($_REQUEST['type']=='sale'){ 
						 $this->data['all_sales_product'] = $this->daily_model->get_product_sale($_REQUEST['id_product'],$_REQUEST['from_date'], $_REQUEST['to_date']);
						//print_r($this->data['all_sales_product']);exit;
						
						$this->load->view('report/reports/all_sales_product',$this->data);	
					}elseif($_REQUEST['type']=='purchase'){
						 $this->data['all_purchase_product'] = $this->daily_model->get_product_purchase($_REQUEST['id_product'],$_REQUEST['from_date'], $_REQUEST['to_date']);
						//print_r($this->data);exit;
						$this->load->view('report/reports/all_purchase_product',$this->data);	
					}elseif($_REQUEST['type']=='stock'){ 
						//echo "stock2";exit;
						$results = $this->daily_model->get_product_stock($_REQUEST['id_product'],$_REQUEST['from_date'], $_REQUEST['to_date']);
						$this->data['stocks']=$results;
						
						
						$this->data['product_code']=$_REQUEST['id_product'];
						$this->data['start_date']=$_REQUEST['from_date'];
						$this->data['end_date']=$_REQUEST['to_date'];
					//print_r($this->data);exit;
						$this->load->view('report/reports/particular_stock_product',$this->data);				
					}	
				}
				
				
			}	
		
			
		}
			
	}
	///////////////////this is the end of the first part for only one date where from date is equal to the rto date	
	
	
	
	//////////////////	
	function get_all_branch(){
		//list the users
		
		$this->data['users'] = $this->branch_model->get_branch()->result();
			
		//			print_r($this->data['users']);exit; 
		// $this->data['user_type'] =  $this->ion_auth_model->get_user_type($this->session->userdata('user_id'))->row();
		//echo $get_user_type->name;
	   
		// exit;
		$meta['page_title'] = "branch List";
		$this->data['message'] = "branch Added Successfully";
		//  $this->session->set_flashdata('message', 'User Created Successfully');
		$this->load->view('common/header', $meta);
		$this->load->view('settings/branch/view_branch',$this->data);
		$this->load->view('common/footer');
	   
	}
	

	function edit_branch($id){
		
		$this->data['page_title'] = "Edit branch";
		//$this->data['groups']=$this->branch_model->get_branch();
		if(!$this->ion_auth->logged_in()){
			redirect('auth', 'refresh');
		}

		$user = $this->branch_model->edit_branch($id)->row();
		//print_r($user);exit;
		//$groups=$this->ion_auth->groups()->result_array();
		//$currentGroups = $this->ion_auth->get_users_groups($id)->result();
		
		//getting the user type ex. team lead 
		//$get_user_type = $this->ion_auth_model->user_type()->result_array();
		//$currentUser = $this->ion_auth_model->get_user_type($id)->result();
		
		//getting the user type ex. fedonic 
		//$get_departments = $this->ion_auth_model->departments()->result_array();
		//$currentDepartments = $this->ion_auth_model->get_user_department($id)->result();
		
		//process the phone number
		//if (isset($user->phone) && !empty($user->phone))
		//	{
		//$user->phone = explode('-', $user->phone);
		//}

		//validate form input
		$this->form_validation->set_rules('name', 'Branch Name', 'required|xss_clean');
		$this->form_validation->set_rules('code', 'Invoice Code', 'required|xss_clean');
		$this->form_validation->set_rules('phone_no', 'Phone Number', 'required|xss_clean');
		$this->form_validation->set_rules('address', 'Address', 'required|xss_clean');
		/*$this->form_validation->set_rules('phone2', 'Second Part of Phone', 'required|xss_clean|min_length[3]|max_length[3]');
		$this->form_validation->set_rules('phone3', 'Third Part of Phone', 'required|xss_clean|min_length[4]|max_length[4]');
		$this->form_validation->set_rules('branch', 'branch Name', 'required|xss_clean');*/
		
		//$this->form_validation->set_rules('groups', 'Groups', 'xss_clean');

		if(isset($_POST) && !empty($_POST)){
			// do we have a valid request?
			/*if ($this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id'))
			{
			show_error('This form post did not pass our security checks.');
			}*/

			$data = array(
				'name' => $this->input->post('name'),
				'code' => $this->input->post('code'),
				'phone_no' => $this->input->post('phone_no'),
				'Address' => $this->input->post('address')
			);
			//print_r($data);
			//print_r($user->id);exit;
			//Update the groups user belongs to
			if($this->form_validation->run() === TRUE){
				$this->branch_model->update($user->id, $data);
				redirect("settings/branch/get_all_branch");
				//check to see if we are creating the user
				//redirect them back to the admin page
			}
		}

		//display the edit user form
		//$this->data['csrf'] = $this->_get_csrf_nonce();

		//set the flash data error message if there is one
		$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

		//pass the user to the view
		$this->data['user'] = $user;
		//$this->data['groups'] = $groups;
		//$this->data['currentGroups'] = $currentGroups;
		
		//user type 
		
		//$this->data['user_type'] = $get_user_type;
		//	$this->data['currentUser'] = $currentUser;
		
		//user departments
		
		//$this->data['get_departments'] = $get_departments;
		//$this->data['currentDepartments'] = $currentDepartments;		
		

		$this->data['name'] = array(
			'name'  => 'name',
			'id'    => 'name',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('name', $user->name),
		);

		$this->data['code'] = array(
			'name'  => 'code',
			'id'    => 'code',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('code', $user->code),
		);




		$this->data['phone_no'] = array(
			'name'  => 'phone_no',
			'id'    => 'phone_no',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('phone_no', $user->phone_no),
		);
		
		$this->data['address'] = array(
			'name'  => 'address',
			'id'    => 'address',
			'type'  => 'textarea',
			'value' => $this->form_validation->set_value('address', $user->address),
		);
		
		/*$this->data['phone2'] = array(
		'name'  => 'phone2',
		'id'    => 'phone2',
		'type'  => 'text',
		'class' => 'span2',
		'value' => $this->form_validation->set_value('phone2', $user->phone[1]),
		);
		$this->data['phone3'] = array(
		'name'  => 'phone3',
		'id'    => 'phone3',
		'type'  => 'text',
		'class' => 'span2',
		'value' => $this->form_validation->set_value('phone3', $user->phone[2]),
		);*/
		
		$this->data['submit'] = array(
			'name' => 'submit',
			'id'   => 'submit',
			'type' => 'submit',
			'value' =>'Save Edit',
			'class' =>'btn btn-success'
		);
		
		//print_r($this->data);exit;
		$this->data['page_title'] = "Edit branch";
		$this->load->view('common/header',$this->data);
		$this->load->view('settings/branch/edit_branch', $this->data, $id);
		$this->load->view('common/footer');
	}

	

	function delete_branch($id){
		$this->branch_model->delete($id);
		redirect("settings/branch/get_all_branch");		
	}
function get_product($id)
{
$this->data['sales_details'] =	$this->daily_model->get_sales_detail($id);
$this->data['detail']  =	$this->daily_model->get_details($id);
//print_r($this->data);exit;
$this->load->view('report/daily_report/sales_product',$this->data);
}

public function get_purchase_detail($id)
{
$this->data['purchase_details'] =	$this->daily_model->get_purchase_detail($id);
$this->data['detail']  =	$this->daily_model->get_purchase_details($id);
//print_r($this->data);exit;
$this->load->view('report/daily_report/purchase_detail',$this->data);

}


}
?>